/**
 * 
 */
package com.pmsco.pms.controller;

import java.text.ParseException;
import java.util.Date;
import java.util.Locale;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pmsco.pms.ajaxdata.AccountInformationJsonData;
import com.pmsco.pms.ajaxdata.SaveAccountInformationJsonData;
import com.pmsco.pms.annotation.LogonRequired;
import com.pmsco.pms.annotation.PermissionRequired;
import com.pmsco.pms.business.UserBusiness;
import com.pmsco.pms.business.dto.UserMainInfoRequestDTO;
import com.pmsco.pms.business.dto.UserMainInfoReturnDTO;
import com.pmsco.pms.core.constant.PmsConstant;
import com.pmsco.pms.util.BundleUtil;
import com.pmsco.pms.util.DateUtils;
import com.pmsco.pms.util.PmsLogonRequired;
import com.pmsco.pms.util.PmsPermissionRequired;
import com.pmsco.pms.util.PmsUserSession;

/**
 * @author Long Tran
 *
 */
@Controller
public class PmsUserMainController extends AbstractPmsController {
	private static final Logger logger = LoggerFactory.getLogger(PmsUserMainController.class);
	
	/**
	 * Load Account Information Action
	 */
	@RequestMapping(value = PmsConstant.accountInformation_Url)
	@LogonRequired(required = PmsLogonRequired.NORMAL_USER)
	@PermissionRequired(required = PmsPermissionRequired.PMS_EDIT_ACCOUNT_INFO_FUNCTION)
	public @ResponseBody AccountInformationJsonData accountInformation(Locale locale, Model model, HttpSession session) {
		logger.info("Begin Load Account Information Action");
		UserBusiness userBusiness = new UserBusiness();
		
		logger.debug("Build load account info request dto");
		UserMainInfoRequestDTO userMainInfoRequestDTO = new UserMainInfoRequestDTO();
		PmsUserSession pmsUserSession = (PmsUserSession)session.getAttribute(PmsConstant.loggedUserSession);
		userMainInfoRequestDTO.setUserName(pmsUserSession.getUserName());
		
		logger.debug("Call to user business to load account info");
		UserMainInfoReturnDTO userMainInfoReturnDTO = userBusiness.loadAccountInfo(userMainInfoRequestDTO);
		
		logger.debug("Analysis load account info return dto");
		AccountInformationJsonData ret = new AccountInformationJsonData();
		ret.setLoggedUser(userMainInfoReturnDTO.getPmsUser());
		
		logger.info("End Load Account Information Action");
		buildMenuList(session);
		return ret;
	}
	
	/**
	 * Save Account Information Action
	 */
	@RequestMapping(value = PmsConstant.saveAccountInformation_Url)
	@LogonRequired(required = PmsLogonRequired.NORMAL_USER)
	@PermissionRequired(required = PmsPermissionRequired.PMS_SAVE_ACCOUNT_INFO_FUNCTION)
	public @ResponseBody SaveAccountInformationJsonData saveAccountInformation(
			@RequestParam("userName") String userName,
			@RequestParam("lastName") String lastName, 
			@RequestParam("midName") String midName, 
			@RequestParam("firstName") String firstName, 
			@RequestParam("idNumber") String idNumber, 
			@RequestParam("dob") String dob, 
			@RequestParam("cellPhone") String cellPhone, 
			@RequestParam("homePhone") String homePhone, 
			@RequestParam("homeAddr") String homeAddr, 
			Locale locale, Model model) {
		logger.info("Begin Save Account Information Action");
		UserBusiness userBusiness = new UserBusiness();
		
		logger.debug("Build save account info request dto");
		SaveAccountInformationJsonData ret = new SaveAccountInformationJsonData();
		UserMainInfoRequestDTO userMainInfoRequestDTO = new UserMainInfoRequestDTO();
		String message = BundleUtil.getInstance().getKey(PmsConstant.saveAccountInformation_SuccessMessage_Key);
		Date userDob;
		try {
			userDob = DateUtils.parseDate(dob);
		} catch (ParseException e) {
			message = BundleUtil.getInstance().getKey(PmsConstant.saveAccountInformation_ErrorDateFormat_Key);
			e.printStackTrace();
			ret.setMessage(message);
			return ret;
		}
		userMainInfoRequestDTO.setUserName(userName);
		userMainInfoRequestDTO.setLastName(lastName);
		userMainInfoRequestDTO.setMidName(midName);
		userMainInfoRequestDTO.setFirstName(firstName);
		userMainInfoRequestDTO.setIdNumber(idNumber);
		userMainInfoRequestDTO.setDob(userDob);
		userMainInfoRequestDTO.setCellPhone(cellPhone);
		userMainInfoRequestDTO.setHomePhone(homePhone);
		userMainInfoRequestDTO.setHomeAddr(homeAddr);
		
		logger.debug("Call to user business to save account info");
		UserMainInfoReturnDTO userMainInfoReturnDTO = userBusiness.saveAccountInfo(userMainInfoRequestDTO);
		ret.setMessage(message);
		
		logger.info("End Save Account Information Action");
		return ret;
	}
}
